package com.by.controller;

import com.by.bean.ValidateBean;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class UserController {
    public Map save(@RequestBody ValidateBean bean, BindingResult result){
        Map map=new HashMap<>();
        //获取验证的错误信息
        List<FieldError> list=result.getFieldErrors();
        if(list.size()>0){
            //说明有错误信息，验证未通过
            map.put("code",0);
            list.forEach(fie->{
                map.put(fie.getField(),fie.getDefaultMessage());
            });
        }
        return map;
    }
}
